﻿using System;
using System.Reflection;

namespace NetFramework.DataAccess.EntityBuilder
{
    internal class BindingInfo
    {
        private PropertyInfo Property;
        private Type propertyType;

        public BindingInfo(string name, PropertyInfo property)
        {
            this.Name = name;
            this.Property = property;
            this.propertyType = property.PropertyType;
        }

        public string Name { get; private set; }

        public void SetValue(object obj, object value)
        {
            this.Property.SetValue(obj, value, null);
        }

        public void SetValue(object obj, object value, Type columnType)
        {
            if (this.propertyType.Equals(columnType))
            {
                this.Property.SetValue(obj, value, null);
            }
            else if(this.propertyType.BaseType.Equals(typeof(Enum)))
            {
                this.Property.SetValue(obj, Enum.Parse(this.propertyType, value.ToString()), null);
            }
            else
            {
                this.Property.SetValue(obj, Convert.ChangeType(value, this.propertyType), null);
            }
        }
    }
}
